import {createSlice} from "@reduxjs/toolkit";
import {request} from "@/utils";

const userStore = createSlice({
    name: 'user',
    initialState: {
        token: ''
    },
    reducers: {
        setToken(state, action) {
            state.token = action.payload;
        },
    },
});

const fetchLogin = (loginForm) => {
    return async (dispatch) => {
        const res = await request.post('/authorizations', loginForm)
        // 保存token
        dispatch(setToken(res.data.token))
    }
}
export {fetchLogin}

//解构
export const {setToken} = userStore.actions;

const userReducer = userStore.reducer;

export default userReducer;
